/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package plugin;

import java.awt.event.ActionEvent;
import java.net.URL;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.gui.layer.markerlayer.ButtonMarker;
import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
import org.openstreetmap.josm.tools.AudioPlayer;

/**
 * AudioButtonMarker extends the JOSM - class ButtonMarker to show
 * kOSMik - captured audio files (.mp4). 
 * <p>
 * It bases on AudioMarker -
 * class which is described on:
 * http://josm.openstreetmap.de/browser/josm/trunk/src/org/
 * openstreetmap/josm/gui/layer/markerlayer/AudioMarker.java
 * <p>
 * @author Andreas
 */
public class AudioButtonMarker extends ButtonMarker{
	
	private URL audioUrl;
	private static AudioButtonMarker recentlyPlayedMarker = null;
	
	/**
	 * Sync offset value.
	 */
	public double syncOffset;
	
	/**
	 * Public boolean.
	 */
	public boolean timeFromAudio = false;

	/**
	 * Constructor.
	 * @param ll contains coordinate latitude and longitude.
	 * @param text - (unimportant).
	 * @param url Audio file.
	 * @param parentLayer The parent layer.
	 * @param time Time stamp of POI (Point of Interest)
	 * @param offset Offset.
	 */
	public AudioButtonMarker(LatLon ll, String text, URL audioUrl,
			MarkerLayer parentLayer, double time, double offset) {
		super(ll, text, "icon_audio.png", parentLayer, time, offset);
		this.audioUrl = audioUrl;
		this.syncOffset = 0.0;
		this.timeFromAudio = false;
	}
	
	/**
	 * Creates an AudioButtonMarker object with the specified values.
	 * @param ll contains coordinate latitude and longitude.
	 * @param text - (unimportant)
	 * @param url Audio file.
	 * @param parentLayer The parent layer.
	 * @param time Time stamp of POI (Point of Interest)
	 * @param offset Offset.
	 * @return An AudioButtonMarker.
	 */
	public static AudioButtonMarker create(LatLon ll, String text, String url, MarkerLayer parentLayer, double time, double offset){
		try{
			return new AudioButtonMarker(ll, text, new URL(url), parentLayer, time, offset);
		} catch(Exception e){
			return null;
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent actionEvent){
		play();
	}
	
	/**
	 * Get the recently played marker.
	 * @return the recently played marker.
	 */
	public static AudioButtonMarker recentlyPlayedMarker(){
		return recentlyPlayedMarker;
	}
	
	/**
	 * Get the audio URL.
	 * @return the audio URL.
	 */
	public URL url(){
		return audioUrl;
	}

	/**
	 * Play the audio file described in audioUrl.
	 */
	public void play() {
		try{
			Main.map.mapView.playHeadMarker.animate();
			AudioPlayer.play(audioUrl, offset + syncOffset);
			recentlyPlayedMarker = this;
		} catch(Exception e){
			AudioPlayer.audioMalfunction(e);
		}
	}
	
	/**
	 * SyncOffset setter.
	 * @param adjustment new value.
	 */
	public void adjustOffset(double adjustment) {
		syncOffset = adjustment;
	}

	/**
	 * SyncOffset getter.
	 * @return syncOffset.
	 */
	public double syncOffset() {
		return syncOffset;
	}
	
	/**
	 * inventName
	 * @param offset -
	 * @return -
	 */
	public static String inventName (double offset) {
		int wholeSeconds = (int)(offset + 0.5);
		if (wholeSeconds < 60)
			return Integer.toString(wholeSeconds);
		else if (wholeSeconds < 3600)
			return String.format("%d:%02d", wholeSeconds / 60, wholeSeconds % 60);
		else
			return String.format("%d:%02d:%02d", wholeSeconds / 3600, (wholeSeconds % 3600)/60, wholeSeconds % 60);
	}

}
